// Copyright (c) 2010 Manuel Peinado Gallego <manuel.peinado@gmail.com>
// Distributed under the MIT license


#include "stdafx.h"
#include "UiFollower.h"
#include "Ui.h"


UiFollower::UiFollower(int flags)
: m_ui(NULL), m_flags(flags)
{
}

UiFollower::~UiFollower()
{
    // TODO use RAII!!
    if(m_ui)
    {
        if((m_flags & UiFollower::IsSelectionListener) != 0)
        {
            m_ui->getSelectionManager().removeSelectionChangeListener(this);
        }
        if((m_flags & UiFollower::IsLogListener) != 0)
        {
            m_ui->getLogManager().removeLogListener(this);
        }
    }
}

// -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  

Ui& UiFollower::getUi()
{
    ASSERT(m_ui != NULL);
    return *m_ui;
}

bool UiFollower::hasUi() const
{
    return m_ui != NULL;
}

void UiFollower::setUi(Ui* ui)
{
    m_ui = ui;
    
    if(m_ui)
    {
        if((m_flags & UiFollower::IsSelectionListener) != 0)
        {
            m_ui->getSelectionManager().addSelectionChangeListener(this);
        }
        if((m_flags & UiFollower::IsLogListener) != 0)
        {
            m_ui->getLogManager().addLogListener(this);
        }
    }

    onUiChanged(ui);
}
